package config

import (
	"crypto/tls"
	"time"
)

// ProxyConfig holds optimized proxy configuration settings
type ProxyConfig struct {
	// Worker pool settings
	WorkerPoolSize int

	// Connection pool settings
	MaxIdleConns        int
	MaxIdleConnsPerHost int
	IdleConnTimeout     time.Duration

	// Timeout settings
	ResponseHeaderTimeout time.Duration
	ExpectContinueTimeout time.Duration
	DialTimeout          time.Duration

	// TLS settings
	TLSClientConfig *tls.Config
	TLSSessionCache tls.ClientSessionCache

	// Buffer settings
	BufferSize int

	// Keep-alive settings
	EnableKeepAlives bool
	DisableCompression bool
}

// DefaultOptimizedConfig returns a pre-configured optimized proxy configuration
func DefaultOptimizedConfig() *ProxyConfig {
	// Create TLS session cache for performance
	tlsSessionCache := tls.NewLRUClientSessionCache(1000)

	return &ProxyConfig{
		WorkerPoolSize:        1000,                           // Number of worker goroutines
		MaxIdleConns:          1000,                           // Maximum total idle connections
		MaxIdleConnsPerHost:   100,                            // Maximum idle connections per host
		IdleConnTimeout:       90 * time.Second,               // How long to keep idle connections
		ResponseHeaderTimeout: 30 * time.Second,               // Timeout for response headers
		ExpectContinueTimeout: 1 * time.Second,                 // Timeout for 100-continue
		DialTimeout:           30 * time.Second,               // Timeout for initial connection
		TLSClientConfig: &tls.Config{
			InsecureSkipVerify: true,
			ClientSessionCache: tlsSessionCache,
		},
		TLSSessionCache:   tlsSessionCache,
		BufferSize:        64 * 1024,                       // 64KB buffers for optimal throughput
		EnableKeepAlives:  true,
		DisableCompression: false,
	}
}

// HighPerformanceConfig returns an aggressive performance configuration
// for high-throughput scenarios
func HighPerformanceConfig() *ProxyConfig {
	config := DefaultOptimizedConfig()

	// More aggressive settings for high performance
	config.WorkerPoolSize = 2000
	config.MaxIdleConns = 2000
	config.MaxIdleConnsPerHost = 200
	config.IdleConnTimeout = 120 * time.Second
	config.BufferSize = 128 * 1024 // 128KB buffers

	return config
}

// LowResourceConfig returns a conservative configuration for resource-constrained environments
func LowResourceConfig() *ProxyConfig {
	tlsSessionCache := tls.NewLRUClientSessionCache(500)

	return &ProxyConfig{
		WorkerPoolSize:        100,
		MaxIdleConns:          100,
		MaxIdleConnsPerHost:   10,
		IdleConnTimeout:       60 * time.Second,
		ResponseHeaderTimeout: 15 * time.Second,
		ExpectContinueTimeout: 500 * time.Millisecond,
		DialTimeout:           15 * time.Second,
		TLSClientConfig: &tls.Config{
			InsecureSkipVerify: true,
			ClientSessionCache: tlsSessionCache,
		},
		TLSSessionCache:   tlsSessionCache,
		BufferSize:        32 * 1024,
		EnableKeepAlives:  true,
		DisableCompression: false,
	}
}